package jjn.round1;

import commons.ListNode;

/**
 * @author Jjn
 * @since 2022/9/5 22:36
 */
public class LeetCode82_RemoveDuplicatesFromLinkedList2 {

    public ListNode deleteDuplicates(ListNode head) {
        if (head == null) {
            return null;
        }
        ListNode dummyHead = new ListNode(0);
        ListNode pre = dummyHead;
        pre.next = head;
        ListNode cur = head;
        while (cur != null && cur.next != null) {
            if (cur.val == cur.next.val) {
                int val = cur.val;
                // 节点值相等，不断往后移动
                while (cur != null && cur.val == val) {
                    cur = cur.next;
                }
                pre.next = cur;
            } else {
                pre = cur;
                cur = cur.next;
            }
        }
        return dummyHead.next;
    }
}
